package oop.core.med;

import java.util.Iterator;
import java.util.List;

import oop.core.Scheduler;
import oop.core.Updateable;
import oop.core.entity.DerivedStats;
import oop.core.skill.ActiveSkill;
import oop.core.skill.InteractiveSkill;
import oop.core.skill.PassiveSkill;
import oop.core.view.AreaPanel;
import oop.core.view.GamePanel;
import oop.core.view.LevelUpPopupMenu;
import oop.core.view.SizeablePanel;

/**
 * This is the class that allows the user to choose which skills they
 * want to levelup when the avatar gets a levelup.
 * 
 * @author Stephanie
 *
 */

public class LevelUpMediator implements Updateable {

	private List<InteractiveSkill> interactive;
	private List<PassiveSkill> passive;
	private List<ActiveSkill> active;
	private int currentLevel;
	private DerivedStats der;
	protected Scheduler sched;
	private LevelUpPopupMenu menu;
	private AreaPanel gamePanel;
	
	/**
	 * Constructor. 
	 * @param sizeablePanel is the panel the popupmenu will render to.
	 * @param sched is the Scheduler that records that this mediator exists and checks for updates
	 * @param der is the derrived stats to get current level information about the avatar from
	 * @param interactive are the interactive skills of the entity
	 * @param passive are the passive skill of the entity
	 * @param active are the actives skills of the entity.
	 */
	public LevelUpMediator( SizeablePanel sizeablePanel, Scheduler sched, DerivedStats der, List<InteractiveSkill> interactive, List<PassiveSkill> passive, List<ActiveSkill> active) {
		this.sched = sched;
		this.sched.registerPersistent(this);
		this.der = der;
		this.currentLevel = der.getLevel();
		this.interactive = interactive;
		this.passive = passive;
		this.active = active;
		this.gamePanel = (AreaPanel) sizeablePanel;
	}
	
	/**
	 * Level up is called from the popup, figures out which skill is associated with the 
	 * string skillName, then levelsup that particular skill.
	 * @param skillName
	 */
	public void levelUp( String skillName ) {
		Iterator<InteractiveSkill> interactiveIter = interactive.iterator();
		while ( interactiveIter.hasNext() ) {
			InteractiveSkill is = interactiveIter.next();
			if ( skillName.equals(is.getName())) {
				is.levelUp();
				return;
			}
		}
		Iterator<PassiveSkill> passiveIter = passive.iterator();
		while ( passiveIter.hasNext() ) {
			PassiveSkill ps = passiveIter.next();
			if ( skillName.equals(ps.getName())) {
				ps.levelUp();
				return;
			}
		}
		Iterator<ActiveSkill> activeIter = active.iterator();
		while ( activeIter.hasNext() ) {
			ActiveSkill as = activeIter.next();
			if ( skillName.equals(as.getName())) {
				as.levelUp();
				return;
			}
		}
	}
	
	public List<InteractiveSkill> getInteractive() {
		return interactive;
	}
	
	public List<PassiveSkill> getPassive() {
		return passive;
	}
	
	public List<ActiveSkill> getActive() {
		return active;
	}

	@Override
	public void update() {
		checkIfLevelingUp();		
	}
	
	/**
	 * This is a constant check invoked by Scheduler. When the avatar
	 * is leveling up, calls PopupMenu and goes from there.
	 */
	private void checkIfLevelingUp() {
		if ( currentLevel < der.getLevel() ) { //Avatar has leveled up
			System.out.println( "trying to pop up menu ");
			popUpMenu();
			currentLevel++;
		}			
	}
	
	private void popUpMenu() {
			menu = new LevelUpPopupMenu( this );
			menu.show( gamePanel,0,0);
	}
}

